package co.fitstart.weapp.api.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.user.StudentTargetData;
import co.fitstart.weapp.api.dao.StudentTargetDao;
import co.fitstart.weapp.api.service.StudentTargetService;

/**
 * 
 * @author haiyan_xiao
 *
 */
@Service("studentTargetService")
public class StudentTargetServiceImpl implements StudentTargetService {

    @Dao
    private StudentTargetDao studentTargetDao;

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.StudentTargetService#list(co.fitstart.entity.course.round.CourseRound)
     */
    public List<StudentTargetData> list(CourseRound courseRound) {
        return studentTargetDao.list(courseRound);
    }
    
    /**
     * 
     * @param courseRound
     * @return
     */
    public StudentTargetData load(CourseRound courseRound) {
        return studentTargetDao.load(courseRound);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.StudentTargetService#cancelFullInTarget(co.fitstart.entity.course.round.CourseRound)
     */
    public void cancelFullInTarget(CourseRound courseRound) {
        genericDaoSupport.update(courseRound.fullInTarget(false));
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.StudentTargetService#create(co.fitstart.entity.course.round.CourseRound, co.fitstart.entity.user.StudentTargetData)
     */
    public void create(CourseRound courseRound, StudentTargetData studentTargetData) {
        if(studentTargetData.getTargetType() == null
                || (studentTargetData.getTargetType().isMuscleBuilding() && (studentTargetData.getBfrChange() == null || studentTargetData.getWeightChange() == null || studentTargetData.getBfrChange() <= 0 || studentTargetData.getBfrChange() > 20 || studentTargetData.getWeightChange() <= 0 || studentTargetData.getWeightChange() > 20))
                || (studentTargetData.getTargetType().isWeightLosing() && (studentTargetData.getWeightChange() == null || studentTargetData.getWeightChange() <= 0 || studentTargetData.getWeightChange() > 20))
                || (studentTargetData.getTargetType().isBodyFatRateReducing() && (studentTargetData.getBfrChange() == null || studentTargetData.getBfrChange() <= 0 || studentTargetData.getBfrChange() > 20))
                || (!studentTargetData.getTargetType().isDigital() && StringUtils.isBlank(studentTargetData.getDescription()))) {
            throw new BusinessException("请求参数有误");
        } else {
            StudentTargetData originalStudentTargetData = studentTargetDao.load(courseRound);
            
            if(originalStudentTargetData == null) {
                genericDaoSupport.save(studentTargetData.initialize(courseRound.getStudent(), courseRound));
                
                genericDaoSupport.update(courseRound.fullInTarget(true));
            } else {
                genericDaoSupport.update(originalStudentTargetData.edit(studentTargetData.getTargetType(), studentTargetData.getWeightChange(), studentTargetData.getBfrChange(), studentTargetData.getDescription()));
            }
        }
    }

}
